/**
 * Filename: ContactGroupUserAdapter.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/3/9 1:22
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNContactUser;
import com.github.postsanf.yinian.constants.CommonConstants;

import java.util.HashMap;
import java.util.List;

import cz.msebera.android.httpclient.client.cache.Resource;

/**
 * 联系人分组列表
 */
public class ContactGroupUserAdapter extends BaseAdapter implements SectionIndexer {
    private List<YNContactUser> lists;
    private Context context;
    // 用来控制CheckBox的选中状况
    private static HashMap<Integer,Boolean> isSelected;
    private Resources mResource;

    public ContactGroupUserAdapter(List<YNContactUser> lists, Context context) {
        this.lists = lists;
        this.context = context;
        isSelected = new HashMap<Integer, Boolean>();
        // 初始化selected
        initDate();
    }

    public void setResource(Resources mResource){
        this.mResource  = mResource;
    }

    // 初始化isSelected的数据
    private void initDate(){
        for(int i=0; i<lists.size();i++) {
            getIsSelected().put(i,false);
        }
    }

    public static HashMap<Integer,Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer,Boolean> isSelected) {
        ContactGroupUserAdapter.isSelected = isSelected;
    }

    //更新列表
    public void updateListView(List<YNContactUser> lists) {
        this.lists = lists;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int position) {
        return lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        final YNContactUser mContent = lists.get(position);

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.yn_item_contact, null);
            holder.item_cb = (CheckBox) convertView.findViewById(R.id.item_cb);
            holder.catalog = (TextView) convertView.findViewById(R.id.contact_catalog);
            holder.contactimg = (ImageView) convertView.findViewById(R.id.contactimg);
            holder.contactname = (TextView) convertView.findViewById(R.id.contactname);
            holder.contactnumber = (TextView) convertView.findViewById(R.id.contactnumber);
            holder.setposition   = (TextView) convertView.findViewById(R.id.setposition);
            if (mResource!=null){
                holder.item_cb.setButtonDrawable(mResource.getDrawable(mResource.getIdentifier("radio_sel", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
            }
            holder.contactname.setText(lists.get(position).getName());
            holder.contactnumber.setText(lists.get(position).getNumber());
            holder.setposition.setText(lists.get(position).getId()+"");
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
            holder.contactname.setText(lists.get(position).getName());
            holder.contactnumber.setText(lists.get(position).getNumber());
            holder.setposition.setText(lists.get(position).getId()+"");
        }

        // 根据position获取分类的首字母的Char ascii值
        int section = getSectionForPosition(position);
        // 判断是否第一次出现,若是则显示分类
        if (position == getPositionForSection(section)) {
            holder.catalog.setVisibility(View.VISIBLE);
            holder.catalog.setText(mContent.getNameLetters());
        } else {
            holder.catalog.setVisibility(View.GONE);
        }
        // 根据isSelected来设置checkbox的选中状况
        holder.item_cb.setChecked(getIsSelected().get(position));
        holder.item_cb.setChecked(getIsSelected().get(lists.get(position).getId()));
        return convertView;
    }

    @Override
    public Object[] getSections() {
        return null;
    }
    // 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
    @Override
    public int getPositionForSection(int sectionIndex) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = lists.get(i).getNameLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == sectionIndex) {
                return i;
            }
        }
        return -1;
    }
    // 根据ListView的当前位置获取分类的首字母的Char ascii值
    @Override
    public int getSectionForPosition(int position) {
        return lists.get(position).getNameLetters().charAt(0);
    }

    // 提取英文的首字母，非英文字母用#代替。
    private String getAlpha(String str) {
        String sortStr = str.trim().substring(0, 1).toUpperCase();
        // 正则表达式，判断首字母是否是英文字母
        if (sortStr.matches("[A-Z]")) {
            return sortStr;
        } else {
            return "#";
        }
    }

    public static class ViewHolder{
        public CheckBox     item_cb;
        public TextView	    catalog;
        public ImageView 	contactimg;
        public TextView 	contactname;
        public TextView 	contactnumber;
        public TextView 	setposition;
    }
}
